package com.xiejr.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author mobai
 * @since 2021/2/16 14:38
 */
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss", ignoreInvalidFields = true)
public class OssConfig implements InitializingBean {

    private String endpoint;

    private String ak;

    private String sk;

    private String bucket;

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("aliyun oss config has already configured ");
        log.info("endpoint=>{},ak=>{},sk=>{},bucket=>{}",endpoint,ak,sk,bucket);
    }

    @Bean
    @ConditionalOnMissingBean
    public OSS oss() {
        return new OSSClientBuilder().build(endpoint, ak, sk);
    }
}
